package controller

import (
	"context"
	v1 "learn-goframe/api/v1"
	"learn-goframe/api/v1/common"
	"learn-goframe/internal/model"
	"learn-goframe/internal/service"
)

type cSolution struct{}

var Solution = cSolution{}

func (c *cSolution) SolutionList(ctx context.Context, req *v1.SolutionListReq) (res *v1.SolutionListRes, err error) {
	out, err := service.Solution().SolutionList(ctx, model.SolutionListInput{
		Page: req.Page,
		Size: req.Size,

		Status:       req.Status,
		CreateUserId: service.BizCtx().Get(ctx).User.Id,
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionListRes{
		List: out.List,
		PaginationRes: common.PaginationRes{
			Total: out.Total,
			Page:  out.Page,
			Size:  out.Size,
		},
	}, nil
}

func (c *cSolution) SolutionSave(ctx context.Context, req *v1.SolutionSaveReq) (res *v1.SolutionSaveRes, err error) {
	info := service.BizCtx().Get(ctx).User.DeveloperInfo

	if req.Id == "" {
		orgName := ""
		if info.CertType == 1 {
			orgName = info.CertInfo.UnitName
		} else if info.CertType == 2 {
			orgName = info.CertInfo.Name
		}

		relName := ""
		if info.CertType == 1 {
			relName = info.CertInfo.Name
		}

		out, err := service.Solution().SolutionUserCreate(ctx, model.SolutionUserCreateInput{
			Id:               req.Id,
			CreateUserId:     service.BizCtx().Get(ctx).User.Id,
			CreateUserName:   service.BizCtx().Get(ctx).User.NickName,
			CreateUserType:   info.CertType,
			OrgName:          orgName,
			RelName:          relName,
			OrgLicenseImgUrl: info.CertInfo.IdCardImageUrl,

			ContactMobile: info.CertInfo.Mobile,
			Status:        req.Status,

			SolutionCreateBaseInput: model.SolutionCreateBaseInput{
				SolutionName:      req.SolutionName,
				SolutionImgUrl:    req.SolutionImgUrl,
				SolutionSummarize: req.SolutionSummarize,
				SolutionFileUrl:   req.SolutionFileUrl,
				CategoryId:        req.CategoryId,
				CategoryParentId:  req.CategoryParentId,
			},
		})

		if err != nil {
			return nil, err
		}
		return &v1.SolutionSaveRes{
			Id: out.Id,
		}, nil
	} else {
		out, err := service.Solution().SolutionUserUpdate(ctx, model.SolutionUserUpdateInput{
			Id:     req.Id,
			Status: req.Status,

			SolutionCreateBaseInput: model.SolutionCreateBaseInput{
				SolutionName:      req.SolutionName,
				SolutionImgUrl:    req.SolutionImgUrl,
				SolutionSummarize: req.SolutionSummarize,
				SolutionFileUrl:   req.SolutionFileUrl,
				CategoryId:        req.CategoryId,
				CategoryParentId:  req.CategoryParentId,
			},
		})

		if err != nil {
			return nil, err
		}
		return &v1.SolutionSaveRes{
			Id: out.Id,
		}, nil
	}
}

func (c *cSolution) SolutionRollback(ctx context.Context, req *v1.SolutionRollbackReq) (res *v1.SolutionRollbackRes, err error) {
	err = service.Solution().SolutionRollback(ctx, model.SolutionSingleIdInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionRollbackRes{}, nil
}

func (c *cSolution) SolutionDelete(ctx context.Context, req *v1.SolutionDeleteReq) (res *v1.SolutionDeleteRes, err error) {
	err = service.Solution().SolutionDelete(ctx, model.SolutionSingleIdInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionDeleteRes{}, nil
}

func (c *cSolution) SolutionSubmit(ctx context.Context, req *v1.SolutionSubmitReq) (res *v1.SolutionSubmitRes, err error) {
	err = service.Solution().SolutionSubmit(ctx, model.SolutionSingleIdInput{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}
	return &v1.SolutionSubmitRes{}, nil
}

func (c *cSolution) CertInfo(ctx context.Context, req *v1.CertInfoReq) (res *v1.CertInfoRes, err error) {
	info := service.BizCtx().Get(ctx).User.DeveloperInfo

	orgName := ""
	if info.CertType == 1 {
		orgName = info.CertInfo.UnitName
	} else if info.CertType == 2 {
		orgName = info.CertInfo.Name
	}

	relName := ""
	if info.CertType == 1 {
		relName = info.CertInfo.Name
	}

	return &v1.CertInfoRes{
		CertType:         info.CertType,
		OrgName:          orgName,
		RelName:          relName,
		ContactMobile:    info.CertInfo.Mobile,
		OrgLicenseImgUrl: info.CertInfo.IdCardImageUrl,
	}, nil
}
